package sk.naive.talker;

import javax.mail.*;
import java.util.*;

/**
 * MailAccountPerUserHelper.
 * @author <a href="mailto:virgo@naive.deepblue.sk">Richard "Virgo" Richter</a>
 * @version $Revision: 1.3 $ $Date: 2007-05-07 10:34:20 $
 */
public class MailAccountPerUserHelper extends MailHelper {
	private Map<Integer, Store> userIdStore;

	public MailAccountPerUserHelper() {
		userIdStore = new HashMap<Integer, Store>();
	}

	private Store getStore(User user) throws MessagingException {
		Store store = userIdStore.get(user.getId());
		if (store == null) {
			Session session = getSession();
			store = session.getStore("imap");
		}
		if (!store.isConnected()) {
			store.connect(getMailStoreHost(), user.getLogin(), (String) user.getProperties().get(User.UPROP_PLAIN_PASSWORD));
			userIdStore.put(user.getId(), store);
		}
		return store;
	}

	protected Folder getRealInbox(User user) throws MessagingException {
		Folder folder = getStore(user).getFolder("INBOX");
		folder.open(Folder.READ_WRITE);
		return folder;
	}
}
